﻿using System.Drawing;
using System.Windows.Forms;
using WeForms.Drawing;

namespace WeForms.Controls
{
    /// <summary>
    /// Lists all color themes of the <see cref="TouchButton"/> control.
    /// </summary>
    public enum TouchButtonColor
    {
        Black,
        Gray,
        Red,
        Violet,
        Blue,
        Cyan,
        Green,
        Yellow,
        Orange,
        WeTab
    }

    /// <summary>
    /// This class implements the drawing behavoiour of the <see cref="TouchButton"/> control.
    /// </summary>
    internal class TouchButtonRenderer
    {
        internal static readonly ControlPaintHelper[] ButtonPainter;
        internal static readonly Brush[] CaptionBrush;
        internal static readonly StringFormat CaptionFormat;
        internal static readonly Font DefaultFont;

        /// <summary>
        /// Initializes the <see cref="TouchButtonRenderer"/> class.
        /// </summary>
        static TouchButtonRenderer()
        {
            ButtonPainter = new ControlPaintHelper[(int)TouchButtonColor.WeTab + 1];
            ButtonPainter[(int)TouchButtonColor.Black] = new ControlPaintHelper(TouchButtonResources.Black, new Size(21, 49),
                                                                           new Padding(10));
            ButtonPainter[(int)TouchButtonColor.Gray] = new ControlPaintHelper(TouchButtonResources.Gray, new Size(21, 49),
                                                                           new Padding(10));
            ButtonPainter[(int)TouchButtonColor.Red] = new ControlPaintHelper(TouchButtonResources.Red, new Size(21, 49),
                                                                           new Padding(10));
            ButtonPainter[(int)TouchButtonColor.Violet] = new ControlPaintHelper(TouchButtonResources.Violet, new Size(21, 49),
                                                                           new Padding(10));
            ButtonPainter[(int)TouchButtonColor.Blue] = new ControlPaintHelper(TouchButtonResources.Blue, new Size(21, 49),
                                                                           new Padding(10));
            ButtonPainter[(int)TouchButtonColor.Cyan] = new ControlPaintHelper(TouchButtonResources.Cyan, new Size(21, 49),
                                                                           new Padding(10));
            ButtonPainter[(int)TouchButtonColor.Green] = new ControlPaintHelper(TouchButtonResources.Green, new Size(21, 49),
                                                                           new Padding(10));
            ButtonPainter[(int)TouchButtonColor.Yellow] = new ControlPaintHelper(TouchButtonResources.Yellow, new Size(21, 49),
                                                                           new Padding(10));
            ButtonPainter[(int)TouchButtonColor.Orange] = new ControlPaintHelper(TouchButtonResources.Orange, new Size(21, 49),
                                                                           new Padding(10));
            ButtonPainter[(int)TouchButtonColor.WeTab] = new ControlPaintHelper(TouchButtonResources.WeTab, new Size(21, 49),
                                                                           new Padding(4));

            CaptionFormat = new StringFormat
            {
                LineAlignment = StringAlignment.Center,
                Alignment = StringAlignment.Center
            };

            CaptionBrush = new Brush[(int)TouchButtonColor.WeTab + 1];
            CaptionBrush[(int)TouchButtonColor.Black] = new SolidBrush(Color.FromArgb(237, 237, 237));
            CaptionBrush[(int)TouchButtonColor.Gray] = new SolidBrush(Color.FromArgb(35, 35, 35));
            CaptionBrush[(int)TouchButtonColor.Red] = CaptionBrush[(int)TouchButtonColor.Black];
            CaptionBrush[(int)TouchButtonColor.Violet] = CaptionBrush[(int)TouchButtonColor.Black];
            CaptionBrush[(int)TouchButtonColor.Blue] = CaptionBrush[(int)TouchButtonColor.Black];
            CaptionBrush[(int)TouchButtonColor.Cyan] = CaptionBrush[(int)TouchButtonColor.Black];
            CaptionBrush[(int)TouchButtonColor.Green] = CaptionBrush[(int)TouchButtonColor.Black];
            CaptionBrush[(int)TouchButtonColor.Yellow] = CaptionBrush[(int)TouchButtonColor.Black];
            CaptionBrush[(int)TouchButtonColor.Orange] = CaptionBrush[(int)TouchButtonColor.Black];
            CaptionBrush[(int) TouchButtonColor.WeTab] = new SolidBrush(Color.FromArgb(233, 233, 233));
            DefaultFont = new Font("Arial", 19, GraphicsUnit.Pixel);
        }

        /// <summary>
        /// Draws the <see cref="TouchButton"/> control. 
        /// </summary>
        /// <param name="button">The button to draw.</param>
        /// <param name="graphic">The graphic to draw into.</param>
        /// <param name="bounds">The bounds to draw into.</param>
        public static void DrawButton(TouchButton button, Graphics graphic, Rectangle bounds)
        {
            // Background
            ControlPaintHelper painter = ButtonPainter[(int)button.ColorScheme];
            int index;
            if (button.Pressed)
            {
                index = 2;
            }
            else if (button.Checked)
            {
                index = 3;
            }
            else if (button.Hovered)
            {
                index = 1;
            }
            else
            {
                index = 0;
            }

            painter.Draw(graphic, bounds, index);

            // Caption
            Brush captionBrush = CaptionBrush[(int)button.ColorScheme];
            graphic.DrawString(button.Text, button.Font, captionBrush, bounds, CaptionFormat);
        }
    }
}
